//Package cmd is a package for parsing command line parameters
//Copyright © 2020 Tim_Paik <timpaik@163.com>
package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/tim-paik/pdk/api"
)

// installCmd represents the install command
var installCmd = &cobra.Command{
	Use:     "install <package(s)>",
	Short:   "Install packages",
	Long:    `Download and install the latest software package`,
	Aliases: []string{"i"},
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			fmt.Println("Error: No target specified for installation")
		}

		repoPath, err := cmd.Flags().GetString("repo")
		if err != nil {
			fmt.Println(err)
		}

		err = api.Install(args, repoPath)
		if err != nil {
			fmt.Println(err)
		}
	},
}

func init() {
	rootCmd.AddCommand(installCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// installCmd.PersistentFlags().String("foo", "", "A help for foo")
	installCmd.PersistentFlags().String("repo", "./repo.json", "Install from the specified repo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// installCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
